﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ManageLiteAV;
using TRTC;
using TRTCCSharpDemo.Common;

namespace TRTCCSharpDemo
{
    public partial class TRTCScreenForm : Form
    {
        private ITRTCCloud mTRTCCloud;

        private TRTCVideoEncParam mEncParam;
        private TRTCNetworkQosParam mQosParams;
        private TRTCAppScene mAppScene;
        private bool mPlaySmallVideo;
        private bool mPushSmallVideo;
        private VideoMainForm mMainForm;
        private ITRTCScreenCaptureSourceList mScreenList;
        private TRTCScreenCaptureSourceInfo mScreenInfo;
        private int mMicVolume = 50;

        public TRTCScreenForm(ITRTCCloud cloud, VideoMainForm form)
        {
            InitializeComponent();

            this.Disposed += new EventHandler(OnDisposed);

            mTRTCCloud = cloud;
            mMainForm = form;
            this.resolutionComboBox.Items.Add("320 x 180");
            this.resolutionComboBox.Items.Add("320 x 240");
            this.resolutionComboBox.Items.Add("640 x 360");
            this.resolutionComboBox.Items.Add("640 x 480");
            this.resolutionComboBox.Items.Add("960 x 540");
            this.resolutionComboBox.Items.Add("1280 x 720");
            this.resolutionComboBox.Items.Add("1920 x 1080");

            this.fpsComboBox.Items.Add("10 fps");
            this.fpsComboBox.Items.Add("15 fps");
            this.fpsComboBox.Items.Add("20 fps");
            this.fpsComboBox.Items.Add("24 fps");

            //this.qosComboBox.Items.Add("优先流畅");
            //this.qosComboBox.Items.Add("优先清晰");
            //checkBoxPlaySystemAudio.Checked = true;
        }

        public bool isSysytemAudoEnabled()
        {
            return checkBoxPlaySystemAudio.Checked;
        }

        private void OnDisposed(object sender, EventArgs e)
        {
            //清理资源
            mScreenList.release();
            mScreenList = null;
            mTRTCCloud = null;
            mMainForm = null;
        }

        #region Form Move

        private bool mIsMouseDown = false;
        private Point mFormLocation;     // Form的location
        private Point mMouseOffset;      // 鼠标的按下位置

        private void OnFormMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mIsMouseDown = true;
                mFormLocation = this.Location;
                mMouseOffset = Control.MousePosition;
            }
        }

        private void OnFormMouseUp(object sender, MouseEventArgs e)
        {
            mIsMouseDown = false;
        }

        private void OnFormMouseMove(object sender, MouseEventArgs e)
        {
            if (mIsMouseDown)
            {
                Point pt = Control.MousePosition;
                int x = mMouseOffset.X - pt.X;
                int y = mMouseOffset.Y - pt.Y;

                this.Location = new Point(mFormLocation.X - x, mFormLocation.Y - y);
            }
        }

        #endregion

        private void OnLoad(object sender, EventArgs e)
        {
            {
                InitData();
                if (mEncParam.videoResolution == TRTCVideoResolution.TRTCVideoResolution_320_180)
                    this.resolutionComboBox.SelectedIndex = 0;
                else if (mEncParam.videoResolution == TRTCVideoResolution.TRTCVideoResolution_320_240)
                    this.resolutionComboBox.SelectedIndex = 1;
                else if (mEncParam.videoResolution == TRTCVideoResolution.TRTCVideoResolution_640_360)
                    this.resolutionComboBox.SelectedIndex = 2;
                else if (mEncParam.videoResolution == TRTCVideoResolution.TRTCVideoResolution_640_480)
                    this.resolutionComboBox.SelectedIndex = 3;
                else if (mEncParam.videoResolution == TRTCVideoResolution.TRTCVideoResolution_960_540)
                    this.resolutionComboBox.SelectedIndex = 4;
                else if (mEncParam.videoResolution == TRTCVideoResolution.TRTCVideoResolution_1280_720)
                    this.resolutionComboBox.SelectedIndex = 5;
                else if (mEncParam.videoResolution == TRTCVideoResolution.TRTCVideoResolution_1920_1080)
                    this.resolutionComboBox.SelectedIndex = 6;

                if (mEncParam.videoFps == 10)
                    this.fpsComboBox.SelectedIndex = 0;
                else if (mEncParam.videoFps == 15)
                    this.fpsComboBox.SelectedIndex = 1;
                else if (mEncParam.videoFps == 20)
                    this.fpsComboBox.SelectedIndex = 2;
                else if (mEncParam.videoFps == 24)
                    this.fpsComboBox.SelectedIndex = 3;

                //if (mQosParams.preference == TRTCVideoQosPreference.TRTCVideoQosPreferenceSmooth)
                //    this.qosComboBox.SelectedIndex = 0;
                //else if (mQosParams.preference == TRTCVideoQosPreference.TRTCVideoQosPreferenceClear)
                //    this.qosComboBox.SelectedIndex = 1;



                int bitrate = (int)mEncParam.videoBitrate;
                this.bitrateTrackBar.Value = bitrate;
                this.bitrateNumLabel.Text = bitrate + " kbps";

                SIZE thumbSize = new SIZE() { cx = 100, cy = 100 };
                SIZE iconSize = new SIZE() { cx = 100, cy = 100 };
                mScreenList = mTRTCCloud.getScreenCaptureSources(ref thumbSize, ref iconSize);
                Log.I(String.Format("ITRTCScreenCaptureSourceList : count = {0}", mScreenList.getCount()));
                for (uint i = 0; i < mScreenList.getCount(); i++)
                {
                    TRTCScreenCaptureSourceInfo sourse = mScreenList.getSourceInfo(i);
                    this.screenComboBox.Items.Add(sourse.sourceName);
                    Log.I(String.Format("ScreenCaoture{0} : type = {1}, sourseId = {2}, sourseName = {3}, thumbBuffer = {4}, iconBuffer = {5}",
                        i + 1, sourse.type, sourse.sourceId, sourse.sourceName, sourse.thumbBGRA.buffer + " {" + sourse.thumbBGRA.width + ", " + sourse.thumbBGRA.height + "}",
                        sourse.iconBGRA.buffer + " {" + sourse.iconBGRA.width + ", " + sourse.iconBGRA.height + "}"));
                }
                this.screenComboBox.SelectedIndex = 0;
            }
        }
        private void InitData()
        {
            mEncParam = new TRTCVideoEncParam()
            {
                videoBitrate = PropertySaver.GetInstance().encParams.videoBitrate,
                videoFps = PropertySaver.GetInstance().encParams.videoFps,
                videoResolution = PropertySaver.GetInstance().encParams.videoResolution,
                resMode = PropertySaver.GetInstance().encParams.resMode
            };
            mQosParams = new TRTCNetworkQosParam()
            {
                preference = PropertySaver.GetInstance().qosParams.preference,
                controlMode = PropertySaver.GetInstance().qosParams.controlMode
            };
            mAppScene = PropertySaver.GetInstance().appScene;
            mPlaySmallVideo = PropertySaver.GetInstance().playSmallVideo;
            mPushSmallVideo = PropertySaver.GetInstance().pushSmallVideo;
        }
        private bool IsChanged()
        {
            if (PropertySaver.GetInstance().encParams.videoResolution != mEncParam.videoResolution)
                return true;
            if (PropertySaver.GetInstance().encParams.videoFps != mEncParam.videoFps)
                return true;
            if (PropertySaver.GetInstance().qosParams.preference != mQosParams.preference)
                return true;
            if (PropertySaver.GetInstance().qosParams.controlMode != mQosParams.controlMode)
                return true;
            if (PropertySaver.GetInstance().appScene != mAppScene)
                return true;
            if (PropertySaver.GetInstance().encParams.videoBitrate != mEncParam.videoBitrate)
                return true;
            if (PropertySaver.GetInstance().playSmallVideo != mPlaySmallVideo)
                return true;
            if (PropertySaver.GetInstance().pushSmallVideo != mPushSmallVideo)
                return true;
            return false;
        }
        private void bitrateTrackBar_Scroll(object sender, EventArgs e)
        {
            int bitrate = this.bitrateTrackBar.Value;
            this.bitrateNumLabel.Text = bitrate + " kbps";
            mEncParam.videoBitrate = (uint)bitrate;
            bool changed = this.IsChanged();
        }
        private void OnSaveBtnClick(object sender, EventArgs e)
        {          
            mTRTCCloud.setSubStreamEncoderParam(ref mEncParam);
            for (uint i = 0; i < mScreenList.getCount(); i++)
            {
                TRTCScreenCaptureSourceInfo sourse = mScreenList.getSourceInfo(i);
                if (sourse.sourceName.Equals(this.screenComboBox.Text))
                {
                    mScreenInfo = sourse;
                }
            }
            RECT rect = new RECT()
            {
                top = 0,
                left = 0,
                right = 0,
                bottom = 0
            };
            mTRTCCloud.selectScreenCaptureTarget(ref mScreenInfo, ref rect, true, true);
            //mMainForm.OnSetScreenParamsCallback(true);
            this.DialogResult = DialogResult.OK;
            this.Hide();
        }

        private void OnCancelBtnClick(object sender, EventArgs e)
        {
            //mMainForm.OnSetScreenParamsCallback(false);
            mTRTCCloud.stopSystemAudioLoopback();
            this.Close();
        }

        private void OnLeftTextBoxKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {
                if (e.KeyChar < 48 || e.KeyChar > 57)
                    e.Handled = true;
            }
        }

        private void OnRightTextBoxKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {
                if (e.KeyChar < 48 || e.KeyChar > 57)
                    e.Handled = true;
            }
        }

        private void OnTopTextBoxKeyPress(object sender, KeyPressEventArgs e)
        {
            //if (e.KeyChar != '\b')
            //{
            //    if (e.KeyChar < 48 || e.KeyChar > 57)
            //        e.Handled = true;
            //}
        }

        private void OnBottomTextBoxKeyPress(object sender, KeyPressEventArgs e)
        {
            //if (e.KeyChar != '\b')
            //{
            //    if (e.KeyChar < 48 || e.KeyChar > 57)
            //        e.Handled = true;
            //}
        }

        private void OnLeftTextBoxTextChanged(object sender, EventArgs e)
        {
            //if(string.IsNullOrEmpty(this.LeftTextBox.Text))
            //{
            //    this.LeftTextBox.Text = "0";
            //}
        }

        private void OnRightTextBoxTextChanged(object sender, EventArgs e)
        {
            //if (string.IsNullOrEmpty(this.rightTextBox.Text))
            //{
            //    this.rightTextBox.Text = "0";
            //}
        }

        private void OnBottomTextBoxTextChanged(object sender, EventArgs e)
        {
            //if (string.IsNullOrEmpty(this.bottomTextBox.Text))
            //{
            //    this.bottomTextBox.Text = "0";
            //}
        }

        private void OnTopTextBoxTextChanged(object sender, EventArgs e)
        {
            //if (string.IsNullOrEmpty(this.topTextBox.Text))
            //{
            //    this.topTextBox.Text = "0";
            //}
        }

        private void checkBoxPlaySystemAudio_CheckedChanged(object sender, EventArgs e)
        {
            //if (checkBoxPlaySystemAudio.Checked)
            //{
            //    mTRTCCloud.startSystemAudioLoopback("");
            //}
            //else
            //{
            //    mTRTCCloud.stopSystemAudioLoopback();
            //}
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void resolutionComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

            int index = this.resolutionComboBox.SelectedIndex;
            switch (index)
            {
                case 0:
                    mEncParam.videoResolution = TRTCVideoResolution.TRTCVideoResolution_320_180;
                    break;
                case 1:
                    mEncParam.videoResolution = TRTCVideoResolution.TRTCVideoResolution_320_240;
                    break;
                case 2:
                    mEncParam.videoResolution = TRTCVideoResolution.TRTCVideoResolution_640_360;
                    break;
                case 3:
                    mEncParam.videoResolution = TRTCVideoResolution.TRTCVideoResolution_640_480;
                    break;
                case 4:
                    mEncParam.videoResolution = TRTCVideoResolution.TRTCVideoResolution_960_540;
                    break;
                case 5:
                    mEncParam.videoResolution = TRTCVideoResolution.TRTCVideoResolution_1280_720;
                    break;
                case 6:
                    mEncParam.videoResolution = TRTCVideoResolution.TRTCVideoResolution_1920_1080; 
                    break;
            }
            bool changed = this.IsChanged();
        }

        private void fpsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

            int index = this.fpsComboBox.SelectedIndex;
            switch (index)
            {
                case 0:
                    mEncParam.videoFps = 10;
                    break;
                case 1:
                    mEncParam.videoFps = 15;
                    break;
                case 2:
                    mEncParam.videoFps = 20;
                    break;
                case 3:
                    mEncParam.videoFps = 24;
                    break;
            }
            bool changed = this.IsChanged();
        }
    }
}
